#
#2008 Julka Lipkova
#

import pygtk
pygtk.require('2.0')
import gtk
import gtk.glade
import gobject

import config
from drawingarea import MyDrawingArea
from linebackground import LineBackground
from entrydialog import EntryDialog

class CreatorTask ( gobject.GObject):
    def __init__(self):
        gobject.GObject.__init__( self )
        self.editingArea = MyDrawingArea()
        self.glade = gtk.glade.XML( config.gladeFile( 'glyphCreator.glade' ) )
        self.editorWidget = self.glade.get_widget( 'creatorMain' )
        self.vscale1 = self.glade.get_widget( 'vscale1' )
        self.vscale2 = self.glade.get_widget( 'vscale2' )
        self.editorWidget.parent.remove( self.editorWidget )
        self.glade.get_widget( 'vboxDrawingArea' ).pack_start( self.editingArea )
        self.lineBg = LineBackground()

        self.glade.signal_autoconnect( self )
#       self.editingArea.connect( 'scribble_finished', self.on_scribble_finished );
        self.editingArea.addBg( self.lineBg )

    def getMainWidget(self):
        return self.editorWidget

#   def on_scribble_finished(self, sender):
#       g = Glyph()
#       g.lines = self.editingArea.lineList

#   def on_btnClear_clicked(self, sender):
#       sbinfo = ''
#       self.emit( 'statusbar_notify', sbinfo )
#       self.editingArea.clear()

#   def on_btnNew_clicked(self, sender):
#       sbinfo = ''
#       self.emit( 'statusbar_notify', sbinfo )
#       self.actualID = glyphDB.getRandomID()
#       self.editingArea.removeBg( self.wholeGlyphBg )
#       del self.wholeGlyphBg
#       self.wholeGlyphBg = WholeGlyphBg( self.actualID )
#       self.editingArea.addBg( self.wholeGlyphBg )
#       self.editingArea.clear()

    def on_vscale1_value_changed(self, range):
# TODO: values according to Didactics of handwriting
        self.vscale1.set_value( int(range.get_value()+0.5)-0.2 )
        self.vscale2.set_value( range.get_value() )
        self.editingArea.setGlyphHeight( (range.get_value() + 0.7) * 57 - 62 )

    def on_vscale2_value_changed(self, range):
        self.vscale2.set_value( int(range.get_value()+0.5)-0.2 )
        selfi.vscale1.set_value( range.get_value() )

    def on_btnAddName_clicked( self, sender ):
        # TODO find toplevel, this should be easier
        w = self.editorWidget
        n = w.get_parent()
        while n is not None:
            w = n
            n = n.get_parent()

        dialog = EntryDialog( "Add new name", w )
        if dialog.run() == gtk.RESPONSE_ACCEPT:
            name = dialog.entry.get_text()
            print 'New name: %s' % (name)
        else:
            print 'New name canceled'
        dialog.hide()

gobject.signal_new( 'statusbar_notify', CreatorTask,
       gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,) )


# vim: set ts=4 sw=4 sts=4 et :
